package com.aquarium.ui.controller;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.module.ModuleController;
import com.aquarium.ui.ConnectDialogActivity;

public class ModuleControllerActivity extends Activity {

	private final static int REQUEST_CONNECT_WATER_MOTOR = 1;
	private final static int REQUEST_CONNECT_RPOTEIN_SKIMMER = 2;
	private final static int CONNECT_SECONDS = 5;

	private ModuleController mModuleController;
	private ArrayList<String> mConnectedModules;

	private View mWaterMotorView;
	private View mProteinSkimmerView;
	private View mUVLightView;
	private View mDosingPumpView;

	private TextView mTitle;

	private RelativeLayout mWMSettingLayout;
	private RelativeLayout mPSSettingLayout;

	private Switch mWMSwitch;
	private Switch mPSSwitch;

	private RadioButton mWMOpenRadioButton;
	private RadioButton mPSOpenRadioButton;
	private RadioButton mWMCloseRadioButton;
	private RadioButton mPSCloseRadioButton;

	private Button mWMDoNowButton;
	private Button mPSDoNowButton;
	private Button mWMSettingButton;
	private Button mPSSettingButton;
	private Button mUVLSettingButton;
	private Button mDPSettingButton;
	private ImageView mBackView;

	private boolean mIsWMCurrentOn;
	private boolean mIsWMCurrentOff;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ModuleController.MESSAGE_COMMAND_REPLY:
				Intent intent = new Intent(
						ConnectDialogActivity.ACTION_CONNECT_SUCESS);
				sendBroadcast(intent);
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.module_ctrl);

		mModuleController = ModuleController.getInstance(this);
		initLayout();
		setSwitchListener();
		setRadioButtonLisetner();
		setButtonListener();
	}

	private void initLayout() {
		mBackView = (ImageView) findViewById(R.id.back);

		// mCancelView = (ImageView) findViewById(R.id.cancel);
		mTitle = (TextView) findViewById(R.id.title);
		mTitle.setText(R.string.module_control);
		mConnectedModules = mModuleController.getConnectedModules();
		// for test
		// mConnectedModules = new ArrayList<String>();
		// mConnectedModules.add(ModuleController.WATER_MOTOR);
		// mConnectedModules.add(ModuleController.PROTEIN_SIMMMER);
		// mConnectedModules.add(ModuleController.UV_LIGHT);
		// mConnectedModules.add(ModuleController.DOSING_PUMP);

		for (String module : mConnectedModules) {
			if (module.equals(ModuleController.WATER_MOTOR)) {
				mWaterMotorView = findViewById(R.id.water_motor);
				mWaterMotorView.setVisibility(View.VISIBLE);
				TextView textView = (TextView) mWaterMotorView
						.findViewById(R.id.module_name);
				mWMSettingLayout = (RelativeLayout) mWaterMotorView
						.findViewById(R.id.auto_setting_layout);
				mWMSwitch = (Switch) mWaterMotorView
						.findViewById(R.id.switch_btn);
				mWMOpenRadioButton = (RadioButton) mWaterMotorView
						.findViewById(R.id.open_radio_btn);
				mWMCloseRadioButton = (RadioButton) mWaterMotorView
						.findViewById(R.id.close_radio_btn);
				mWMDoNowButton = (Button) mWaterMotorView
						.findViewById(R.id.do_now_btn);
				mWMSettingButton = (Button) mWaterMotorView
						.findViewById(R.id.time_setting_btn);
				textView.setText(ModuleController.WATER_MOTOR);

				String mode = mModuleController.getWaterMotorMode();
				if (ModuleController.PREFER_VALUE_MODULE_AUTO.equals(mode)) {
					mWMSwitch.setChecked(true);
				} else if (ModuleController.PREFER_VALUE_MODULE_MANU
						.equals(mode)) {
					mWMSwitch.setChecked(false);
				}
				boolean isOn = mModuleController.isWaterMotorOn();
				if (isOn) {
					mWMOpenRadioButton.setChecked(true);
				} else {
					mWMCloseRadioButton.setChecked(true);
				}
				if (mWMSwitch.isChecked()) {
					mWMSettingButton.setVisibility(View.VISIBLE);
					mWMSettingLayout.setVisibility(View.GONE);

				} else {
					mWMSettingButton.setVisibility(View.GONE);
					mWMSettingLayout.setVisibility(View.VISIBLE);
				}
			} else if (module.equals(ModuleController.PROTEIN_SIMMMER)) {
				mProteinSkimmerView = findViewById(R.id.protein_skimmer);
				mProteinSkimmerView.setVisibility(View.VISIBLE);
				TextView textView = (TextView) mProteinSkimmerView
						.findViewById(R.id.module_name);
				mPSSettingLayout = (RelativeLayout) mProteinSkimmerView
						.findViewById(R.id.auto_setting_layout);
				mPSSwitch = (Switch) mProteinSkimmerView
						.findViewById(R.id.switch_btn);
				mPSOpenRadioButton = (RadioButton) mProteinSkimmerView
						.findViewById(R.id.open_radio_btn);
				mPSCloseRadioButton = (RadioButton) mProteinSkimmerView
						.findViewById(R.id.close_radio_btn);
				mPSDoNowButton = (Button) mProteinSkimmerView
						.findViewById(R.id.do_now_btn);
				mPSSettingButton = (Button) mProteinSkimmerView
						.findViewById(R.id.time_setting_btn);
				textView.setText(ModuleController.PROTEIN_SIMMMER);
				String mode = mModuleController.getProteinSkimmerMode();
				if (ModuleController.PREFER_VALUE_MODULE_AUTO.equals(mode)) {
					mPSSwitch.setChecked(true);
				} else if (ModuleController.PREFER_VALUE_MODULE_MANU
						.equals(mode)) {
					mPSSwitch.setChecked(false);
				}
				boolean isOn = mModuleController.isProteinSkimmerOn();
				if (isOn) {
					mPSOpenRadioButton.setChecked(true);
				} else {
					mPSCloseRadioButton.setChecked(true);
				}
				if (mPSSwitch.isChecked()) {
					mPSSettingButton.setVisibility(View.VISIBLE);
					mPSSettingLayout.setVisibility(View.GONE);
				} else {
					mPSSettingButton.setVisibility(View.GONE);
					mPSSettingLayout.setVisibility(View.VISIBLE);
				}
			} else if (module.equals(ModuleController.UV_LIGHT)) {
				mUVLightView = findViewById(R.id.uv_light);
				mUVLightView.setVisibility(View.VISIBLE);
				TextView textView = (TextView) mUVLightView
						.findViewById(R.id.module_name);
				mUVLSettingButton = (Button) mUVLightView
						.findViewById(R.id.time_setting_btn);
				textView.setText(ModuleController.UV_LIGHT);

			} else if (module.equals(ModuleController.DOSING_PUMP)) {
				mDosingPumpView = findViewById(R.id.dosing_pump);
				mDosingPumpView.setVisibility(View.VISIBLE);
				TextView textView = (TextView) mDosingPumpView
						.findViewById(R.id.module_name);
				mDPSettingButton = (Button) mDosingPumpView
						.findViewById(R.id.time_setting_btn);
				textView.setText(ModuleController.DOSING_PUMP);
			}

		}
		// String mode = mModuleController.getWaterMotorMode();
		// if (ModuleController.PREFER_VALUE_MODULE_AUTO.equals(mode)) {
		// mWMSwitch.setChecked(true);
		// } else if (ModuleController.PREFER_VALUE_MODULE_MANU.equals(mode)) {
		// mWMSwitch.setChecked(false);
		// }
		// mode = mModuleController.getProteinSkimmerMode();
		// if (ModuleController.PREFER_VALUE_MODULE_AUTO.equals(mode)) {
		// mPSSwitch.setChecked(true);
		// } else if (ModuleController.PREFER_VALUE_MODULE_MANU.equals(mode)) {
		// mPSSwitch.setChecked(false);
		// }

		// boolean isOn = mModuleController.isWaterMotorOn();
		// if (isOn) {
		// mWMOpenRadioButton.setChecked(true);
		// } else {
		// mWMCloseRadioButton.setChecked(true);
		// }

		// isOn = mModuleController.isProteinSkimmerOn();
		// if (isOn) {
		// mPSOpenRadioButton.setChecked(true);
		// } else {
		// mPSCloseRadioButton.setChecked(true);
		// }

		// if (mPSSwitch.isChecked()) {
		// mPSSettingButton.setVisibility(View.VISIBLE);
		// mPSSettingLayout.setVisibility(View.GONE);
		// } else {
		// mPSSettingButton.setVisibility(View.GONE);
		// mPSSettingLayout.setVisibility(View.VISIBLE);
		// }

		// if (mWMSwitch.isChecked()) {
		// mWMSettingButton.setVisibility(View.VISIBLE);
		// mWMSettingLayout.setVisibility(View.GONE);
		//
		// } else {
		// mWMSettingButton.setVisibility(View.GONE);
		// mWMSettingLayout.setVisibility(View.VISIBLE);
		// }

	}

	private void setSwitchListener() {
		if (mWMSwitch != null) {
			mWMSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {

					if (isChecked) {
						mWMSettingButton.setVisibility(View.VISIBLE);
						mWMSettingLayout.setVisibility(View.GONE);
						mModuleController
								.setWaterMotorMode(ModuleController.PREFER_VALUE_MODULE_AUTO);
					} else {
						mWMSettingButton.setVisibility(View.GONE);
						mWMSettingLayout.setVisibility(View.VISIBLE);
						mModuleController
								.setWaterMotorMode(ModuleController.PREFER_VALUE_MODULE_MANU);
					}
				}
			});
		}
		if (mPSSwitch != null) {
			mPSSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (isChecked) {
						mPSSettingButton.setVisibility(View.VISIBLE);
						mPSSettingLayout.setVisibility(View.GONE);
						mModuleController
								.setProteinSkimmerMode(ModuleController.PREFER_VALUE_MODULE_AUTO);
					} else {
						mPSSettingButton.setVisibility(View.GONE);
						mPSSettingLayout.setVisibility(View.VISIBLE);
						mModuleController
								.setProteinSkimmerMode(ModuleController.PREFER_VALUE_MODULE_MANU);
					}
				}
			});
		}
	}

	private void setRadioButtonLisetner() {
		// mWMOpenRadioButton
		// .setOnCheckedChangeListener(new OnCheckedChangeListener() {
		//
		// @Override
		// public void onCheckedChanged(CompoundButton compoundbutton,
		// boolean isChecked) {
		// // TODO
		// }
		// });
		// mPSOpenRadioButton
		// .setOnCheckedChangeListener(new OnCheckedChangeListener() {
		//
		// @Override
		// public void onCheckedChanged(CompoundButton compoundbutton,
		// boolean isChecked) {
		// // TODO
		// }
		// });
	}

	private void setButtonListener() {
		if (mWMDoNowButton != null) {
			mWMDoNowButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.putExtra(ConnectDialogActivity.KEY_CONNECT_SECONDS,
							CONNECT_SECONDS);
					intent.setClass(ModuleControllerActivity.this,
							ConnectDialogActivity.class);
					startActivityForResult(intent, REQUEST_CONNECT_WATER_MOTOR);

					String moduleName = getResources().getString(
							R.string.water_motor);
					if (mWMOpenRadioButton.isChecked()) {
						// mModuleController.setWaterMotorOnOff(true);
						mModuleController.sendModuleOnOffCommand(moduleName,
								true, true, mHandler);
					} else if (mWMCloseRadioButton.isChecked()) {
						// mModuleController.setWaterMotorOnOff(false);
						mModuleController.sendModuleOnOffCommand(moduleName,
								false, true, mHandler);
					}
				}
			});
		}
		if (mPSDoNowButton != null) {
			mPSDoNowButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.putExtra(ConnectDialogActivity.KEY_CONNECT_SECONDS,
							CONNECT_SECONDS);
					intent.setClass(ModuleControllerActivity.this,
							ConnectDialogActivity.class);
					startActivityForResult(intent,
							REQUEST_CONNECT_RPOTEIN_SKIMMER);

					String moduleName = getResources().getString(
							R.string.protein_skimmer);
					if (mPSOpenRadioButton.isChecked()) {
						// mModuleController.setProteinSkimmerOnOff(true);
						mModuleController.sendModuleOnOffCommand(moduleName,
								true, true, mHandler);
					} else if (mPSCloseRadioButton.isChecked()) {
						// mModuleController.setProteinSkimmerOnOff(false);
						mModuleController.sendModuleOnOffCommand(moduleName,
								false, true, mHandler);
					}
				}
			});
		}

		if (mWMSettingButton != null) {
			mWMSettingButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.setClass(ModuleControllerActivity.this,
							WaterMotorSetting.class);
					startActivity(intent);
				}
			});
		}
		if (mPSSettingButton != null) {
			mPSSettingButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.setClass(ModuleControllerActivity.this,
							ProteinSkimmerSetting.class);
					startActivity(intent);
				}
			});
		}
		if (mUVLSettingButton != null) {
			mUVLSettingButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.setClass(ModuleControllerActivity.this,
							UVLightSetting.class);
					startActivity(intent);
				}
			});
		}
		if (mDPSettingButton != null) {
			mDPSettingButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent intent = new Intent();
					intent.setClass(ModuleControllerActivity.this,
							DosingPumpSetting.class);
					startActivity(intent);
				}
			});
		}
		mBackView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_OK:
			switch (requestCode) {
			case REQUEST_CONNECT_WATER_MOTOR:
				boolean isWMChecked = mWMOpenRadioButton.isChecked();
				mModuleController.setProteinSkimmerOnOff(isWMChecked);
				break;
			case REQUEST_CONNECT_RPOTEIN_SKIMMER:
				boolean isPSChecked = mPSOpenRadioButton.isChecked();
				mModuleController.setProteinSkimmerOnOff(isPSChecked);
				break;
			default:
				break;
			}
			break;
		case RESULT_CANCELED:
			switch (requestCode) {
			case REQUEST_CONNECT_WATER_MOTOR:
				boolean isWMChecked = mWMOpenRadioButton.isChecked();
				if (isWMChecked) {
					mWMCloseRadioButton.setChecked(true);
				} else {
					mWMOpenRadioButton.setChecked(false);
				}
				break;
			case REQUEST_CONNECT_RPOTEIN_SKIMMER:
				boolean isPSChecked = mPSOpenRadioButton.isChecked();
				if (isPSChecked) {
					mPSCloseRadioButton.setChecked(true);
				} else {
					mPSOpenRadioButton.setChecked(false);
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}
}
